﻿using AIStudio.Util.Primitives;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace AIStudio.WeChat.Client
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入用户名");
            string userName = Console.ReadLine();
            var userId = userName;
            var avatar = userName;

            IWSocketClient _wSocketClient = new WSocketClient();
            _wSocketClient.MessageReceived -= _wSocketClient_MessageReceived;
            _wSocketClient.MessageReceived += _wSocketClient_MessageReceived;
            _wSocketClient.InitAndStart($"http://localhost:5000", $"ws://localhost:5000/ws?userName={userName}&userId={userId}&avatar={avatar}");
            System.Threading.Thread.Sleep(1000);

            while (true)
            {
                Console.WriteLine("请输入接收者");
                string receiveName = Console.ReadLine();
                Console.WriteLine("请输入信息");
                string message = Console.ReadLine();

                MessageResult send = new MessageResult();
                send.Success = true;
                send.MessageType = WSMessageType.MessageType;
                send.Data = new D_UserMessageDTO
                {
                    CreateTime = DateTime.Now,
                    Text = message,
                    CreatorId = userId,
                    CreatorName = userName,
                    UserIds = receiveName
                };

                _wSocketClient.SendMessage(JsonConvert.SerializeObject(send));
                System.Threading.Thread.Sleep(1000);
            }
            Console.ReadKey();
        }

        private static void _wSocketClient_MessageReceived(WSMessageType type, string message)
        {
            if (type == WSMessageType.OnlineUser)
            {
                Console.WriteLine("在线用户：");
                Console.WriteLine(message);
            }
            else if(type == WSMessageType.MessageType)
            {
                Console.WriteLine("收到消息：");
                Console.WriteLine(message);
            }
        }
    }

    public class User
    {
        string UserName { get; set; }
    }
}
